﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using AnesSystem.Services;
using MongoDB.Bson;
using System.Collections.ObjectModel;
using Common;
using AnesSystem.Converters;
using AnesSystem.Util;

namespace AnesSystem.ViewModels
{
    public class HistoryCtrlViewModel : BaseViewModel<HistoryCtrlViewModel>
    {
        #region Variables
        private List<PatientLinkModel> _patientLinkModelList = new List<PatientLinkModel>();
        private List<string> AccountName = new List<string>();

        private ObservableCollection<SelPatientLinkModel> _displayOprList = new ObservableCollection<SelPatientLinkModel>();
        //手术间
        private ConfigOperationRoomModel _ssj = new ConfigOperationRoomModel();
        //住院号
        private string _zyh = "";
        //病人姓名
        private string _brxm = "";
        //医生姓名
        private string _ysxm = "";
        //开始和结束时间
        private DateTime _startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        private DateTime _endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
        private SelPatientLinkModel selectedValue;
        #endregion

        #region Constructor
        public HistoryCtrlViewModel()
        {
        }
        #endregion

        #region Properties
        public List<PatientLinkModel> PatientLinkModelList
        {
            get
            {
                return _patientLinkModelList;
            }

            set
            {
                _patientLinkModelList = value;
                RaisePropertyChanged("DisplayOprList");
            }
        }
        public IEnumerable<ConfigOperationRoomModel> DisplayConfigOperationRoomModelList
        {
            get
            {
                ObservableCollection<ConfigOperationRoomModel> list = new ObservableCollection<ConfigOperationRoomModel>();
                list.Add(new ConfigOperationRoomModel());
                return list.Union(DataManager.Instance.ConfigOperationRoomModelList);
            }
        }

        public ObservableCollection<SelPatientLinkModel> DisplayOprList
        {
            get
            {
                DisplaySeach();
                return _displayOprList;
            }

            set
            {
                _displayOprList = value;
                RaisePropertyChanged("DisplayOprList");
            }
        }

        public ConfigOperationRoomModel Ssj
        {
            get
            {
                if (string.IsNullOrEmpty(_ssj.Name))
                    _ssj.Name = "";
                return _ssj;
            }

            set
            {
                _ssj = value;
                RaisePropertyChanged("Ssj");
                //RaisePropertyChanged("DisplayOprList");
            }
        }

        public string Zyh
        {
            get
            {
                return _zyh;
            }

            set
            {
                _zyh = value;
                RaisePropertyChanged("Zyh");
            }
        }

        public string Brxm
        {
            get
            {
                return _brxm;
            }

            set
            {
                _brxm = value;
                RaisePropertyChanged("Brxm");
            }
        }

        public string Ysxm
        {
            get
            {
                return _ysxm;
            }

            set
            {
                _ysxm = value;
                RaisePropertyChanged("Ysxm");
            }
        }

        public DateTime StartTime
        {
            get
            {
                return _startTime;
            }

            set
            {
                _startTime = value;
                RaisePropertyChanged("StartTime");
            }
        }

        public DateTime EndTime
        {
            get
            {
                return _endTime;
            }

            set
            {
                _endTime = value;
                RaisePropertyChanged("EndTime");
            }
        }

        public SelPatientLinkModel SelectedValue
        {
            get
            {
                return selectedValue;
            }
            set
            {
                if (value == null) return;
                selectedValue = value;
                selectedValue.IsChecked = true;
                DataManager.Instance.CurrentPatientLink = value.PatientLink;
                RaisePropertyChanged("SelectedValue");
            }
        }
        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void DisplaySeach()
        {
            try
            {
                _displayOprList.Clear();
                IEnumerable<PatientLinkModel> lst = PatientLinkModelList.Where(
                    O =>
                    {
                        string trueName = "";
                        //string sts;
                        //将麻醉医生ID转换成TrueName；
                        trueName += DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.Zmys);
                        trueName += DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.Fmys1);
                        trueName += DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.Fmys2);
                        trueName += DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.Fmys3);
                        trueName += DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.OperDoctor1);
                        trueName += DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.OperDoctor2);
                        trueName += DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.OperDoctor3);
                        if (string.IsNullOrEmpty(Ssj.Name))
                        {
                            return O.Patient.Brxm.IsContain(Brxm) && O.Patient.Zyh.Contains(Zyh) && trueName.IsContain(Ysxm);
                        }
                        return O.Schedule.Pbssjh.Equals(Ssj.Name) && O.Patient.Brxm.IsContain(Brxm) && O.Patient.Zyh.Contains(Zyh) && trueName.Contains(Ysxm);
                    }
                    );
                foreach (var item in lst)
                {
                    SelPatientLinkModel model = new SelPatientLinkModel();
                    model.PatientLink = item;
                    if (DataManager.Instance.CurrentPatientLink != null && DataManager.Instance.CurrentPatientLink.Id.Equals(item.Id))
                    {
                        SelectedValue = model;
                    }
                    _displayOprList.Add(model);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
            }
        }

        #endregion

        #region Public/Protected Methods

        public void InitData()
        {
            //PatientLinkModelList = PatientLinkService.Instance.GetHistoryTableOpers();
        }

        public void Excute(string comand)
        {
            switch (comand)
            {
                case "cx":
                    QueryList();
                    break;
                case "zj":
                    RecentList();
                    break;
                case "dc":
                    ExportList();
                    break;
            }
        }

        #endregion

        #region EventHandlers

        #endregion


        public void QueryList()
        {
            PatientLinkModelList = PatientLinkService.Instance.GetHistoryOperations(StartTime, EndTime, Ssj.Name, Zyh, Brxm).OrderByDescending(O => O.Status.InRoomTime).ToList();
        }

        public void RecentList()
        {
            EndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            StartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day - 3 < 0 ? 1 : DateTime.Now.Day - 3, 0, 0, 0);
            PatientLinkModelList = PatientLinkService.Instance.GetHistoryOperations(StartTime, EndTime, Ssj.Name, Zyh, Brxm).ToList();
        }


        public void ExportList()
        {
            //DateTime startTime = StartTime.AddDays();
            //DateTime endTime = EndTime;
            if (PatientLinkModelList == null || PatientLinkModelList.Count == 0)
            {
                MessageBoxCommon.ShowDialog("请先查询病人！", MessageBoxCommon.OK);
                return;
            }
            else
            {
                ExportToExcel ex = new ExportToExcel();
                ex.ExportParameterCommon("历史手术", PatientLinkModelList);

            }            
        }
    }

}
